package Boke;
class Counter1{
    public int cout;
    public synchronized void increase(){
        cout++;
    }

    public int getCout(){
        return cout;
    }
}

public class SameUpdate {
    public static void main(String[] args) throws InterruptedException {
        Counter1 counter1=new Counter1();
        System.out.println("目标结果是：100000");
        //线程t1让cout成员自增到5w
        Thread t1=new Thread(()->{
            for (int i = 0; i < 5_0000; i++) {
                counter1.increase();
            }

        });
        t1.start();
        //线程t2让cout成员自增到5w
        Thread t2=new Thread(()->{
            for (int i = 0; i < 5_0000; i++) {
                counter1.increase();
            }
        });

        t2.start();

        //等待两个线程都结束
        t1.join();
        t2.join();
        System.out.println("多线程处理下实际结果为："+counter1.getCout());
    }
}
